/*
 * PopupMessageWidget.hpp
 *
 *  Created on: Jul 11, 2012
 *      Author: guillou
 */

#ifndef POPUPMESSAGEWIDGET_HPP_
#define POPUPMESSAGEWIDGET_HPP_

#include <map>
#include <PopupLibTypes.hpp>
#include <PopupGui.hpp>
#include <PopupImageViewer.hpp>
#include <QMutex>
#include <QTabWidget>
#include <QMessageBox>

class PopupThreadStartWidget;
class PopupThreadWidget;
class PopupCanvassWidget;

class PopupMessageWidget : public QTabWidget
{
  Q_OBJECT

public:
  PopupMessageWidget(QWidget *parent);
  virtual ~PopupMessageWidget() {}

  void getThreadWidgets(std::vector<const PopupThreadWidget*>&);
  void addThread(const PopupGui::PopupThreadSave & p_saved);

public slots:
  // IN
  void slot_messageReceived(Popup::UserMessage p_message);
  void slot_messageSent(Popup::UserMessage p_message);
  void slot_messageReactionReceived(Popup::RateUserMessage p_reaction);
  void slot_onOpinionRequested(Popup::Canvass p_canvass);
  void slot_onVoteReceived(Popup::Vote p_vote);
  void slot_onInvitationReceived(Popup::ThreadInvitation p_invitation);
  // OUT
  void slot_sendMessage(Popup::UserMessage *p_message);
  void slot_sendMessageReaction(Popup::RateUserMessage *p_reaction);
  void slot_sendVote(Popup::Vote *p_vote);
  void slot_sendInvitation(Popup::ThreadInvitation *p_invitation);
  // Settings update
  void slot_applyNewSettings();

private slots:
  void slot_showMessage(const Popup::UserMessage *p_message,
                        PopupThreadWidget *p_widget);
  void slot_onStart();
  void slot_closeCurrentThread();
  void slot_currentChanged(int p_index);
  void slot_showImage(const QString & p_image,
                      const QString & p_sender = "");

signals:
  void signal_showMessage(Popup::UserMessage *p_message);
  void signal_sendMessage(Popup::UserMessage *p_message);
  void signal_sendMessageReaction(Popup::RateUserMessage *p_reaction);
  void signal_submitCanvass(Popup::Canvass *p_canvass);
  void signal_sendVote(Popup::Vote *p_vote);
  void signal_sendInvitation(Popup::ThreadInvitation *p_invitation);
  void signal_browseAttachment();
  void signal_showImage(const QString & p_image,
                        const QString & p_sender = "",
                        bool p_forceFullscreen = false);
  void signal_applyNewSettings();

private:
  bool displayAuthorized(PopupGui::ReceivedMsgType p_type =
      PopupGui::POPUP_RECVD_MESSAGE);
  void highlightWidget(QWidget *p_widget, bool p_show = false);
  void startThread();
  void startCanvass();
  void processNewMessage(const Popup::UserMessage & p_message);
  PopupThreadWidget *createNewThread(const Popup::ThreadID & p_threadID,
                                     const QString & p_threadTitle,
                                     const Popup::UserList & p_targets =
                                         Popup::EmptyUserList,
                                     bool p_isThreadTitleValid = true,
                                     const QString & p_text = "");

private:

  typedef std::map<Popup::ThreadID, PopupThreadWidget*> PopupThreadWidgetMap;
  typedef std::map<Popup::ThreadID, PopupCanvassWidget*> PopupCanvassWidgetMap;

  QWidget *m_parent;
  PopupThreadWidgetMap m_threads;
  PopupCanvassWidgetMap m_opinionRequests;
  std::set<int> m_highlightedTabs;
  QMutex m_mutex;
  QMutex m_promptMutex;
  unsigned int threadCounter;
  unsigned int anonymousThreadCounter;
  PopupThreadStartWidget *m_threadStartWidget;
  QMessageBox m_messageBox;
};

#endif /* POPUPMESSAGEWIDGET_HPP_ */
